package com.easyidea.its.util;



/**
 * Util Class: ParamsUtilities. <br>
 * Use this class to deal with the special string which include html tag (like "<",
 * ">","&" ""","'" ).
 * 
 * @author jeecyhuang
 * @createtime 2007-5-30
 * 
 * Remove from CEM Common at 2008-05-16
 */
public final class ParamsUtilities {
	private ParamsUtilities() {
	}

	/**
	 * Converts a text string to HTML.
	 * 
	 * @param text
	 *            The string to convert.
	 * 
	 * @return The converted string.
	 */
	public static String textToHTML(String text) {
		if (text == null) {
			return "null";
		}
		final StringBuffer html = new StringBuffer();
		for (int i = 0; i < text.length(); i++) {
			// Convert character to HTML/XML
			html.append(charToHTML(text.charAt(i)));
		}

		// Return HTML/XML string
		return html.toString();
	}

	/**
	 * Converts a character to HTML.
	 * 
	 * @param ch
	 *            The character to convert.
	 * 
	 * @return The converted string.
	 */
	private static String charToHTML(char ch) {

		if (ch == '<') {
			return ("&lt;");
		} else if (ch == '>') {
			return ("&gt;");
		} else if (ch == '&') {
			return ("&amp;");
		} else if (ch == '"') {
			return ("&quot;");
		} else if (ch == '\'') {
			return ("&#39;");
		} else {
			return (String.valueOf(ch));
		}
	}

	/**
	 * convert a html str to text str
	 * 
	 * @author Andy
	 * @param str
	 * @return str
	 */
	public static String htmlToText(String str) {
		if (str == null) {
			return "";
		}
		String strResult = str.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&amp;", "&").replaceAll(
				"&quot;", "\"").replaceAll("&#39;", "\'");
		return strResult;
	}

	/**
	 * Replace reserved char of sql.
	 * 
	 * @param str
	 * @return
	 */
	public static String dealWithReservedSqlChar(String str) {
		String result = str;
		if (!StringUtil.isNullOrBlank(str)) {
			result = result.replace("\\", "\\\\");
			result = result.replace("\'", "\\'");
			result = result.replace("%", "\\%");
			result = result.replace("_", "\\_");
		}
		return result;
	}

	/**
	 * Add by Jeecy on 20081111 -- on version 1.7<br>
	 * Replace reserved char of mysql equal sql.
	 * 
	 * @param str
	 * @return
	 */
	public static String dealWithReservedEqualSqlChar(String str) {
		String result = str;
		if (!StringUtil.isNullOrBlank(str)) {
			result = result.replace("\\", "\\\\");
			result = result.replace("\'", "\\'");
		}
		return result;
	}
	public static String convertChar(char ch) {

		if (ch == '?') {
			return ("_");
		} else if (ch == '*') {
			return ("%");
		} else {
			return (String.valueOf(ch));
		}
	}
}
